﻿using System.Data.SqlTypes;
using System.Text;
using System.Threading;

namespace ConsoleApp1
{
    public abstract class TestThread
    {
        private Thread _thread;
        private Mutex _mutex;
        private bool _pauseFlag;
        private bool _stopFlag;
        private State _state;

        public TestThread()
        {
            _thread = null;
            _pauseFlag = false;
            _stopFlag = false;
            _state = State.Stoped; 
        }

        public enum State
        {
            Stoped,
            Running,
            Paused
        };

        public State ReadState()
        {
            return _state;
        }

        public void Start()
        {
            if (_thread != null) return;
            _thread = new Thread(new ThreadStart(run));
            _pauseFlag = false;
            _stopFlag = false;
            _thread.Start();
            _state = State.Running;
        }

        public void Stop()
        {
            if (_thread == null) return;
            _pauseFlag = false;
            _stopFlag = true;
            _thread = null;
            _state = State.Stoped;
        }

        public void Pause()
        {
            if (_thread == null) return;
            _pauseFlag = true;
            _state = State.Paused;
        }

        public void Resume()
        {
            if (_thread == null) return;
            _pauseFlag = false;
            _state = State.Running;

        }

        protected abstract void Process();

        private void run()
        {
            while (!_stopFlag)
            {
                Process();
                while (_pauseFlag)
                {
                   Thread.Sleep(10);
                   if (_stopFlag)
                       break;
                }
            }

            _pauseFlag = false;
            _stopFlag = false;
        }
    }
}